package com.liangzhong.model.tourMatch
{
	import com.liangzhong.constants.ShareConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.events.ShareEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.tourMatch.vo.TourJoinMatchVo;
	import com.liangzhong.model.tourMatch.vo.TourMatchEndVo;
	import com.liangzhong.model.tourMatch.vo.TourMatchStatusVo;
	
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *巡回赛服务器数据解析 
	 * @author lake
	 * 
	 */	
	public class TourMatchServerDataParse
	{
		public function TourMatchServerDataParse()
		{
		}
		/**
		 *巡回赛状态 
		 * @param data
		 * 
		 */		
		public static function readTourMatchStatus(data : ByteArray):TourMatchStatusVo
		{
			var statusVo :TourMatchStatusVo = new TourMatchStatusVo();
			
			statusVo.ret = data.readInt();
			if(statusVo.ret != 0)
			{
				return statusVo;
			}
			statusVo.level = data.readByte();
			AppData.getInstance().tourMatchManager.curLevel = statusVo.level;
			statusVo.lastLevel = data.readByte();
			statusVo.matchNum = data.readByte();
			var resObj :Object;
			for(var i :int = 0;i < 8;i++)
			{
				resObj = new Object();
				resObj.id = data.readInt();
				resObj.result = data.readByte();
				resObj.sNum   = data.readByte();
				resObj.oNum	  = data.readByte();
				//resObj.result = 1;
				statusVo.resultList.push(resObj);
			}
			
			return statusVo;
		}
		/**
		 *参赛返回信息 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readTourJoinMatch(data : ByteArray):TourJoinMatchVo
		{
			var tourJoinVo :TourJoinMatchVo = new TourJoinMatchVo();
			
			tourJoinVo.ret = data.readInt();
			if(tourJoinVo.ret != 0)
			{
				//Alert.showErrorTip("报名失败 code = " + tourJoinVo.ret);
				return tourJoinVo;
			}
			var statusVo :TourMatchStatusVo = AppData.getInstance().tourMatchManager.tourMatchStatus;
			statusVo.level = AppData.getInstance().tourMatchManager.curLevel;
			statusVo.matchNum = 0;
			statusVo.resultList = [];
			var resObj :Object;
			for(var i :int = 0;i < 8;i++)
			{
				resObj = new Object();
				resObj.id = data.readInt();
				resObj.result = 0;
				tourJoinVo.ids.push(resObj);
				statusVo.resultList.push(resObj);
			}
			
			return tourJoinVo;
		}
		/**
		 *本轮巡回赛结束 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readTourMatchEnd(data : ByteArray):TourMatchEndVo
		{
			var tourMatchEndVo :TourMatchEndVo = new TourMatchEndVo();
			var statusVo :TourMatchStatusVo = AppData.getInstance().tourMatchManager.tourMatchStatus;
			tourMatchEndVo.isSuccess = data.readByte();
			var awLen :int = data.readShort();
			var i :int = 0;
			var pid :int;
			var num :int;
			while(i < awLen)
			{
				pid = data.readInt();
				num = data.readInt();
				tourMatchEndVo.awards.push(pid);
				i ++ ;
			}
			//tourMatchEndVo.awards = AppData.getInstance().tourMatchManager.tourMatchStatus.curAwards;
			if(statusVo)
			{
				if(tourMatchEndVo.isSuccess == 1)
				{
					if(statusVo.isPass() == false && statusVo.lastLevel < 4)
					{
						statusVo.lastLevel ++ ;
					}
					//share
					var shareParam :Object = {};
					switch(statusVo.level)
					{
						case 1:
						{
							shareParam.type = ShareConst.TourMatch_Cross_C;
							break;
						}
						case 2:
						{
							shareParam.type = ShareConst.TourMatch_Cross_B;
							break;
						}
						case 3:
						{
							shareParam.type = ShareConst.TourMatch_Cross_A;
							break;
						}
						case 4:
						{
							shareParam.type = ShareConst.TourMatch_Cross_S;
							break;
						}
					}
					AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.TOURMATCH_DATA_CHANGE,{level:statusVo.level});
					AppEventDispatcher.getInstance().dispatchEvent(new ShareEvent(ShareEvent.SHARE_SAY,shareParam));
				}
				statusVo.level = 0;
			}
			return tourMatchEndVo;
		}
	}
}